package com.zzsy.carosshopprojects.activitys.home.mainpage.main;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.base.FunctionTypeEnum;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.ConfigBean;
import com.zzsy.carosshopprojects.bean.HomeContentBean;
import com.zzsy.carosshopprojects.bean.HomeContentResultBean;
import com.zzsy.carosshopprojects.bean.HomePageBean;
import com.zzsy.carosshopprojects.bean.OrderListBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.AlgorithmUtil;
import com.zzsy.carosshopprojects.util.ApkInfoUtil;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.ShowDialogUtils;
import com.zzsy.carosshopprojects.view.dialog.ComOneBtnDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.MutableLiveData;

/**
 * 首页VM类
 */
public class MainPageMode extends BaseViewMode {
    private MutableLiveData<List<HomePageBean>> liveDataHpBean;//首页列表数据
    private MutableLiveData<HomeContentResultBean> liveDataHcBean;//首页数据
    //吧台
    private int[] iconBar = {R.mipmap.item_member_open_card, R.mipmap.item_member_cz, R.mipmap.item_quan_hx, R.mipmap.receipt_img, R.mipmap.bu_cards, R.mipmap.fu_card, R.mipmap.kouchu};
    private String[] aliasBar = {FunctionTypeEnum.MEMBEROPENCARD, FunctionTypeEnum.MEMBERCHARGE, FunctionTypeEnum.COUPONWRITEOFF, FunctionTypeEnum.INVOICE, FunctionTypeEnum.CARDMANAGER, FunctionTypeEnum.SUBCARDMANAGER, FunctionTypeEnum.INTEGRALDEDUCTION};
    //加油员
    private int[] icon = {R.mipmap.item_shou, R.mipmap.item_member_xf, R.mipmap.bu_car, R.mipmap.one_add_oils,R.mipmap.quickjoinoil};//R.mipmap.quickjoinoil
    private String[] alias = {FunctionTypeEnum.CHARGE, FunctionTypeEnum.MEMBERCONSUME, FunctionTypeEnum.SENDCARD, FunctionTypeEnum.ONEKEYOIL,FunctionTypeEnum.QUICKJOINOIL};//FunctionTypeEnum.QUICKJOINOIL

    /**
     * 根据登录的角色获取对应的功能列表数据
     *
     * @param
     */
    public void postHomePageListBean() {
        List<HomePageBean> list = new ArrayList<>();
        if (DataUtil.getInstance().getCustomerType().equals("2")) {//吧台
            for (int i = 0; i < iconBar.length; i++) {
                HomePageBean homePageBean = new HomePageBean();
                homePageBean.setAlias(aliasBar[i]);
                homePageBean.setIcon(iconBar[i]);
                list.add(homePageBean);
            }

        } else if (DataUtil.getInstance().getCustomerType().equals("1")) {//加油员
            for (int i = 0; i < icon.length; i++) {
                HomePageBean homePageBean = new HomePageBean();
                homePageBean.setAlias(alias[i]);
                homePageBean.setIcon(icon[i]);
                list.add(homePageBean);
            }
        }
        liveDataHpBean.postValue(list);
    }

    /**
     * 获取首页数据
     *
     * @param context
     */
    public void postHomeContentBean(Context context) {
        RequestUtils.HomeContent(context, DataUtil.getInstance().getTerminalId(), new MyObserver<HomeContentBean>(context) {
            @Override
            public void onSuccess(HomeContentBean bean) {
                if (bean == null) {
                    ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.MainError));
                    return;
                }
                HomeContentResultBean homeContentResultBean = new HomeContentResultBean();
                homeContentResultBean.setOrderAmountStr(AlgorithmUtil.getAmount(bean.getOrderAmount(), 100));
                homeContentResultBean.setPayAmount(AlgorithmUtil.getAmount(bean.getPayAmount(), 100));
                homeContentResultBean.setRechargeAmount(AlgorithmUtil.getAmount(bean.getRechargeAmount(), 100));
                homeContentResultBean.setOrderCount(bean.getOrderCount() == null ? "" : bean.getOrderCount());
                homeContentResultBean.setTerminalName(DataUtil.getInstance().getTerminalName());
                if (DataUtil.getInstance().getCustomerType().equals("1")) {
                    homeContentResultBean.setLeftTitle("订单金额");
                    homeContentResultBean.setRightTitle("订单数量");
                    homeContentResultBean.setLeftAccount("￥" + homeContentResultBean.getOrderAmountStr());
                    homeContentResultBean.setRightAccount(homeContentResultBean.getOrderCount());
                    if (bean.getOrder() != null) {
                        homeContentResultBean.setShowTopView(true);
                        homeContentResultBean.setCreateTime(AlgorithmUtil.getTime(bean.getOrder().getCreateTime(), 1000));
                        homeContentResultBean.setOilNum(bean.getOrder().getOilNum());
                        homeContentResultBean.setOrderAmount(AlgorithmUtil.getAmount(bean.getOrder().getOrderAmount(), 100));
                        homeContentResultBean.setProductName(bean.getOrder().getProductName());
                        homeContentResultBean.setOilsData("加" + bean.getOrder().getOilNum() + bean.getOrder().getProductName() + homeContentResultBean.getOrderAmount() + "元");
                    } else {
                        homeContentResultBean.setShowTopView(false);
                    }
                } else if (DataUtil.getInstance().getCustomerType().equals("2")) {
                    homeContentResultBean.setLeftTitle("充值金额");
                    homeContentResultBean.setRightTitle("订单金额");
                    homeContentResultBean.setLeftAccount("￥" + homeContentResultBean.getRechargeAmount());
                    homeContentResultBean.setRightAccount("￥" + homeContentResultBean.getRechargeAmount());
                    homeContentResultBean.setShowTopView(false);
                }
                liveDataHcBean.postValue(homeContentResultBean);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.MainError));
            }
        });
    }

    /**
     * 获取首页列表数据的LiveData
     *
     * @return
     */
    public MutableLiveData<List<HomePageBean>> getLiveDataHp() {
        if (liveDataHpBean == null)
            liveDataHpBean = new MutableLiveData<>();
        return liveDataHpBean;
    }

    /**
     * 获取首页数据的LiveData
     *
     * @return
     */
    public MutableLiveData<HomeContentResultBean> getLiveDataHc() {
        if (liveDataHcBean == null)
            liveDataHcBean = new MutableLiveData<>();
        return liveDataHcBean;
    }


    /**
     * 判断是否更新 强制不强制更新
     */
    public void getVersion(Activity activity, Context context) {
        String versionName = ApkInfoUtil.getVersionName(context);
        RequestUtils.getConfig(context, new MyObserver<ConfigBean>(context, false) {
            @Override
            public void onSuccess(ConfigBean result) {
                if (result != null) {
                    String currentVersion = result.getVersionDTO().getCurrentVersion();
                    String forcedUpdate = result.getVersionDTO().getForcedUpdate();
                    int isUpdate = ApkInfoUtil.compareVersion(currentVersion, versionName);
                    if (isUpdate == 1) {
                        if ("1".equals(forcedUpdate)) {
                            /**
                             * 设置强制更新对话窗
                             * */
                            ShowDialogUtils.showDialogs(activity, ComOneBtnDialog.DIALOG_VERSIONS, 1);
                        } else {
                            /**
                             * 设置非强制更新对话窗
                             * */
                            ShowDialogUtils.showDialogs(activity, ComOneBtnDialog.DIALOG_NOT_UPDATE, -1);

                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                Log.d("LLLL", "0000pabbbbb");
            }
        });
    }


    /**
     * 获取油站未打印订单小票
     *
     * @param context
     */
    public void IsHaveTicketPrint(Context context) {
        RequestUtils.IsHaveTicketPrint(context, DataUtil.getInstance().getTerminalId(), new MyObserver<List<OrderListBean>>(context, false) {
            @Override
            public void onSuccess(List<OrderListBean> beans) {
                isPrintTicket(context, beans);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {

            }
        });
    }

    /**
     * 查询是否可以打印小票
     *
     * @param beans
     */
    private void isPrintTicket(Context context, List<OrderListBean> beans) {
        if (beans == null) {
            return;
        }

        for (OrderListBean bean : beans) {
            RequestUtils.UpdatePrintStatus(context, bean.getOrderNumber(), new MyObserver<BaseBean>(context, false) {
                @Override
                public void onSuccess(BaseBean result) {
                 //   PrintHelper.getInstance().printOrderTicket(bean);
                }

                @Override
                public void onFailure(Throwable e, String errorMsg, int code) {
                    if (code == ConfigureFile.code) {
                     //   PrintHelper.getInstance().printOrderTicket(bean);
                    }
                }
            });
        }

    }


}
